<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/19
 * Time: 16:49
 */
namespace app\common\model;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;
use think\Request;

class BaseModel extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;
    protected static $model = null;
    public static function instance(){
        if(empty(self::$model)){
            self::$model = new static();
        }
        return self::$model;
    }

    public function buildParams(Request $request){
        $where = [];
        $page = $request->get('page',DATA_NORMAL);
        $limit = $request->get('limit',LIMIT);
        $order = $request->get('order','ASC');
        $sort = $request->get('sort','id');
        $search_param = $request->except(['page','limit','order','sort'],'get');
        $fields = Db::name($this->getName())->getTableFields();
        foreach ($search_param as $param=>$item){
            if($item == '') continue;
            if(in_array($param,$fields)){
                if(is_numeric($param)) {
                    $where[] = [$param, '=',$item];
                }else{
                    $where[] = [$param,'like',"%{$item}%"];
                }
            }
        }
        return [$where,$sort,$order,$page,$limit];
    }
}